package com.runrunfast.fxj.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.runrunfast.fxj.entity.SysResources;
import com.runrunfast.fxj.entity.SysRole;
import com.runrunfast.fxj.enums.ResourcesEnum;
import com.runrunfast.fxj.enums.RoleStatusEnum;
import com.runrunfast.fxj.enums.UserStatusEnum;
import com.runrunfast.fxj.exception.FxjException;
import com.runrunfast.fxj.form.SysResourcesForm;
import com.runrunfast.fxj.form.SysResourcesUpdateForm;
import com.runrunfast.fxj.vo.ResultVo;
import com.runrunfast.fxj.entity.SysUser;
import com.runrunfast.fxj.mapper.SysUserMapper;
import com.runrunfast.fxj.service.ISysResourcesService;
import com.runrunfast.fxj.utils.ResultVOUtil;
import com.runrunfast.fxj.vo.SysResourcesTreeVo;
import com.runrunfast.fxj.vo.SysRoleAddVo;
import com.runrunfast.fxj.vo.SysUserVo;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 权限表 前端控制器
 * </p>
 *
 * @author 陈胤训
 * @since 2019-03-12
 */
@Slf4j
@Api(value = "权限",tags={"权限接口"})
@RestController
@RequestMapping("/resources")
public class SysResourcesController {

    @Autowired
    private ISysResourcesService resourcesService;



    @ApiResponses({
            @ApiResponse(code=400,message="请求参数没填好"),
            @ApiResponse(code=404,message="请求路径没有或页面跳转路径不对"),
            @ApiResponse(code = 200, message = "成功返回的records", response = SysResourcesTreeVo.class)
    })
    @ApiOperation(value = "获取权限列表",notes="获取权限列表")
    @PostMapping("/list")
    public ResultVo list(){
        List<SysResources> list = resourcesService.list();


        return   ResultVOUtil.success(list);
    }

    @ApiResponses({
            @ApiResponse(code=400,message="请求参数没填好"),
            @ApiResponse(code=404,message="请求路径没有或页面跳转路径不对"),
            @ApiResponse(code = 200, message = "成功返回的records", response = SysResourcesTreeVo.class)
    })
    @ApiOperation(value = "获取菜单列表(选择父级菜单用)",notes="获取菜单列表(选择父级菜单用)")
    @PostMapping("/selectTreeList")
    public ResultVo selectTreeList(){
        List<SysResourcesTreeVo> list = resourcesService.tree();

        return   ResultVOUtil.success(list);
    }

    @ApiOperation("权限添加")
    @PostMapping("/add")
    public ResultVo add( @Validated SysResourcesForm sysResourcesForm
            , BindingResult bindingResult){
        if (bindingResult.hasErrors()) {
            log.error("【角色添加】参数不正确, orderForm={}", sysResourcesForm);
            log.error(bindingResult.getFieldError().getDefaultMessage());
            throw new FxjException(5,bindingResult.getFieldError().getDefaultMessage());
        }

        resourcesService.add(sysResourcesForm);

        return ResultVOUtil.success();
    }


    @ApiOperation("修改权限信息")
    @PostMapping("/update")
    public ResultVo update(SysResourcesUpdateForm updateForm){
        if (resourcesService.updateInfo(updateForm)){
            throw new FxjException(ResourcesEnum.RESOURCES_NULL.getCode(),ResourcesEnum.RESOURCES_NULL.getDesc());
        }
        return ResultVOUtil.success();
    }

    @ApiOperation("权限修改界面")
    @GetMapping("/updatePage")
    public ResultVo updatePage(@RequestParam("resourcesId") @ApiParam("权限id") Integer resourcesId){
        SysResources byId = resourcesService.getById(resourcesId);

        if (StringUtils.isEmpty(byId)){
            throw new FxjException(ResourcesEnum.RESOURCES_NULL.getCode(),ResourcesEnum.RESOURCES_NULL.getDesc());
        }
        return ResultVOUtil.success(byId);
    }

    @ApiOperation("根据角色获取树结构")
    @GetMapping("/TreeListByRoleId")
    public ResultVo TreeListByRoleId(@RequestParam("roleId") @ApiParam("角色id") Integer roleId){
        List<SysResourcesTreeVo> list = resourcesService.TreeListByRoleId(roleId);


        return ResultVOUtil.success(list);
    }

    @ApiOperation("删除权限及子权限")
    @DeleteMapping("delete")
    public ResultVo delete(@RequestParam("resourcesId") @ApiParam("权限id") Integer resourcesId){
        SysResources byId = resourcesService.getById(resourcesId);
        if (StringUtils.isEmpty(byId)){
            throw new FxjException(ResourcesEnum.RESOURCES_NULL.getCode(),ResourcesEnum.RESOURCES_NULL.getDesc());
        }
        resourcesService.delete(byId);
        return ResultVOUtil.success();
    }

}
